﻿using System;
using System.Threading;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace NetShips {
    /// <summary>
    /// One point of game field.
    /// </summary>
    public class FieldPoint {
        private GameField field;
        /// <summary>
        /// Represents hit status of point.
        /// </summary>
        public enum State_ENUM {
            /// <summary>
            /// Point was not hit.
            /// </summary>
            NotHit,
            /// <summary>
            /// Point was hit.
            /// </summary>
            Hit,
            /// <summary>
            /// We don't know if it is hit.
            /// </summary>
            Unknown

        };

        /// <summary>
        /// What types can point be.
        /// </summary>
        public enum FieldPointType_ENUM {
            /// <summary>
            /// Point as part of/whole ship.
            /// </summary>
            Ship,
            /// <summary>
            /// Point as a sea.
            /// </summary>
            Sea,
            /// <summary>
            /// Wrong placed ship.
            /// </summary>
            Error

        };

        private Point position;
        private Point _size;
        private bool _visibility = false;
        private State_ENUM _state;
        private FieldPointType_ENUM _type;

        /// <summary>
        /// Sets base parameters.
        /// </summary>
        /// <param name="position">Position of point. Index in row and column as a unit.</param>
        /// <param name="size">Height and Width of point in pixels.</param>
        /// <param name="homeField">GameField to which this point belongs.</param>
        internal FieldPoint(Point position, Point size, GameField homeField) {
            field = homeField;
            Type = FieldPointType_ENUM.Sea;
            Pozice = position;
            _size = size;

        }
        /// <summary>
        /// Draws point according to its type and state.
        /// </summary>
        /// <param name="g">Object Graphics used to draw.</param>
        public void Draw(ref Graphics g) {
            Brush brush = null;
            Pen pen = null;

            if(_visibility)
                switch(this._type) {
                    /**
                     * --> Ship <--
                     **/
                    case FieldPointType_ENUM.Ship:

                        /**
                         * --> Hit Ship <--
                         **/
                        if(State == State_ENUM.Hit) {
                            if(this.field.Ships.Get_Reference(this).State == 0) {
                                /**
                                 * --> Whole ship destroyed <--
                                 **/
                                brush = new SolidBrush(Color.FromArgb(124, 124, 124));
                                pen = new Pen(Color.Black);
                            }
                            else {
                                /**
                                 * --> Hit only part <--
                                 **/
                                brush = new SolidBrush(Color.FromArgb(255, 134, 13));
                            }
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        }

                        /**
                         * --> NotHit Ship <--
                         **/
                        if(State == State_ENUM.NotHit) {

                            brush = new SolidBrush(Color.FromArgb(100, 50, 0));
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        }

                        if(State == State_ENUM.Unknown) {
                            brush = new SolidBrush(Color.BlanchedAlmond);
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        }
                        break;

                    /**
                     * --> Water <--
                     **/
                    case FieldPointType_ENUM.Sea:
                        /**
                         * --> Hit Water <--
                         **/
                        if(State == State_ENUM.Hit) {
                            brush = new SolidBrush(Color.FromArgb(116, 187, 252));
                            pen = new Pen(Color.LightBlue, 2);
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                            g.DrawLine(pen, position.X * _size.X, position.Y * _size.Y, position.X * _size.X + _size.X, position.Y * _size.Y + _size.Y);
                            g.DrawLine(pen, position.X * _size.X + _size.X, position.Y * _size.Y, position.X * _size.X, position.Y * _size.Y + _size.Y);
                        }
                        /**
                         * --> NotHit Water <--
                         **/
                        if(State == State_ENUM.NotHit) {
                            brush = new SolidBrush(Color.FromArgb(116, 187, 252));
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        }
                        if(State == State_ENUM.Unknown) {
                            brush = new SolidBrush(Color.BlanchedAlmond);
                            g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        }
                        break;

                    /**
                     *  --> Error <--
                     **/
                    case FieldPointType_ENUM.Error:
                        brush = new SolidBrush(Color.Red);
                        g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
                        break;
                    default:
                        return;
                }
            else {
                brush = new SolidBrush(Color.FromArgb(116, 187, 252));
                g.FillRectangle(brush, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
            }

            pen = new Pen(Color.Black);
            g.DrawRectangle(pen, position.X * _size.X, position.Y * _size.Y, _size.X, _size.Y);
        }

        /// <summary>
        /// Distance from top left corner. Starts with [0,0].
        /// </summary>
        public System.Drawing.Point Pozice {
            get {
                return position;
            }
            set {
                position = value;
            }
        }

        /// <summary>
        /// Represents field points state: Hit, NotHit, Unknown
        /// </summary>
        public State_ENUM State {
            get {
                return _state;
            }
            set {
                _state = value;
            }
        }

        /// <summary>
        /// Sets type to field point: Ship, Watter, Error.
        /// </summary>
        public FieldPointType_ENUM Type {
            get {
                return _type;
            }
            set {
                _type = value;

            }
        }
        /// <summary>
        /// Sets field point size.
        /// </summary>
        public Point Size {
            get {
                return _size;
            }
            set {
                _size = value;
            }
        }

        public bool Visibility {
            get {
                return _visibility;
            }
            set {
                _visibility = value;
            }
        }

    }
}
